//结算单审核详情


interface IFeeSettleAuditDetailViewModel extends IBaseViewModel<IFeeSettleAuditDetailQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceFeeListItemResponse>;
    /**
     * 应付结算总单详情数据
     */
    feeData:FeeSettleBillDetailResponse;
    /**
     * 银行卡是否修改
     */
    cardIsChange:boolean;
    /**
     * 是否单次结算
     */
    isOneFee:boolean;
    /**
     * 审核通过
     */
    audited: ()=> void;
    /**
     * 退回结算
     */
    returnSettle: ()=> void;
    /**
     * 跳转费用对账页面
     */
    checkDetail: (id:string)=> void;

    /**
     * 承运单编号
     */
    carrierOrderId:string;
    /**
     * 费用状态
     */
    sFeeStatusList:string;
    /**
     * 列表高
     */ 
    heightA:number;

    //
    back:()=>void;
}

/**
 * 查询条件
 */
interface IFeeSettleAuditDetailQueryTerms{
    /**
     * 费用编号
     */
    feeCode:string;
    /**
     * 合同号
     */
    contractNumber:string;
    /**
     * 结算承运商
     */
    carrierName:string;
    /**
     * 车牌号
     */
    carCode:string;
}

class IFeeSettleAuditDetailController extends ListController<IFeeSettleAuditDetailViewModel,IFeeSettleAuditDetailQueryTerms> {
    gridApi: uiGrid.IGridApiOf<FinanceFeeListItemResponse>;
    
    asc:boolean = true;
    orderBy:string = "";
    //银行卡是否修改
    cardIsChange = false;
    //是否单次结算
    isOneFee = false;
    //费用状态
    sFeeStatusList ="0,1,2,3,4,5,6,7,8,9,10,11";
    //可识别结算总单编号
    billCode:string = "";
    /**
     * 结算编号
     */
    settleId:string;
    /**
     * 名称
     */
    name:string;

    skip:number = 0;
    count:number = 10;



    constructor($scope:IFeeSettleAuditDetailViewModel, private feeService:IFeeService, private $location:ng.ILocationService, 
    private orderService:IorderService, private $ngBootbox: BootboxService, private $state: angular.ui.IStateService ){
        super($scope);
        this.init();
    }

    init():void{
        super.init();

        this.settleId = this.$location.search().settleId;
        this.name = this.$location.search().name;

        this.$scope.queryTerms = {
            feeCode:"",
            contractNumber:"",
            carrierName:"",
            carCode:""
        }


        this.$scope.gridOptions = {
            enableSorting: false,//禁止所有表头自动排序。
			paginationPageSizes: [10, 20, 30],
			useExternalPagination: true,
			useExternalSorting: true,
			enableColumnResizing: true,
			columnDefs: [
                            { displayName: "费用编号", field: 'feeCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                            { displayName: "合同号", field: 'contractNumber', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '承运商', field: 'carrier', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                            { displayName: "承运商号码", field: 'carrierPhone', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                            { displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                            { displayName: '应付单价', field: 'payablePriceStr', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                            { displayName: '应付说明', field: 'expenditureReasons', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '付款方式', field: 'paymentMethod', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '费用类型', field: 'feeType', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                            { displayName: '费用金额', field: 'feeAmount', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '费用状态', field: 'feeStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '申领人员', field: 'applicant', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '申请日期', field: 'creationTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '结算日期', field: 'settleTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{ displayName: '付款日期', field: 'payTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
							{
								name: "操作",
								cellFilter: 'checkmark',
								cellTemplate: '<a data-toggle="modal" title="费用对账" class="fa fa-eye m-l-xs  primary text-info" ng-click="grid.appScope.checkDetail(row.entity.id)"></a>', enableColumnMenu: false
							}
			],
             onRegisterApi:(gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }            
        };

        /**
         * 应付结算总单详情数据
         */
        this.getDetail();
        this.$scope.audited = this.audited;
        this.$scope.returnSettle = this.returnSettle;
        this.$scope.checkDetail = this.checkDetail;

        this.$scope.back = this.back;

    }

    /**
     * 加载数据
     */
    loadData(queryTerms:IFeeSettleAuditDetailQueryTerms,skip:number,count:number):void{
        this.feeService.getFinanceFeeList(this.orderBy,this.asc,this.billCode ,this.queryTerms.feeCode,"","","",this.queryTerms.contractNumber,"","","","","","","","","","","","",
        this.queryTerms.carrierName,"",this.queryTerms.carCode,"",this.sFeeStatusList,"","","","",skip,count).then((result)=>{
            // this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;

                this.$scope.heightA = (this.$scope.gridOptions.data.length *39) +96;
                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
         });
    }
    
    getListCount():void{
        this.feeService.getFinanceFeeCount(this.billCode ,this.$scope.queryTerms.feeCode,"","","",this.$scope.queryTerms.contractNumber,"","","","","","","","","","","","",
        this.$scope.queryTerms.carrierName,"",this.$scope.queryTerms.carCode,"",this.sFeeStatusList,"","","","").then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }
    /**
     * 应付结算总单详情数据
     */
    getDetail=()=>{
        this.feeService.getFeeSettleBillDetail(this.settleId).then((res)=>{
            this.$scope.feeData = res.data;
            this.billCode = res.data.billCode;
            if(res.data.isChangeBank == true ){
                this.cardIsChange = true;
            }
            if(res.data.isFeeSingle == 'true' ){
                this.isOneFee = true;
            }
            this.query();
        });
    }
   

    /**
     * 审核通过
     */
    audited=()=>{
        this.$ngBootbox.confirm('确定审核通过吗?').then(()=>{
            this.feeService.audited(this.settleId).then((res)=>{
                this.$state.go('app.fee.feeSettle_admin',{name:'feeSettle_admin'});
            });
        });
    }

    /**
     * 退回结算
     */
    returnSettle=()=>{
        this.$ngBootbox.confirm('确定退回结算吗?').then(()=>{
            this.feeService.returnSettle(this.settleId).then((res)=>{
                this.$state.go('app.fee.feeSettle_admin',{name:'feeSettle_admin'});
            });
        });
    }

    /**
     * 费用对账
     */
    checkDetail=(id:string)=>{
        this.feeService.feeDetail(id).then((res)=>{
            this.$scope.carrierOrderId = res.data.carrierOrderId;
            this.orderService.carryOrder(this.$scope.carrierOrderId).then((result)=>{
                if(result.data.orderList.length > 1 ){
                    this.$state.go('app.fee.feeBreakbulkDetail',{ feeId:id, carrierOrderId:this.$scope.carrierOrderId, pageName:'settleAuditManage' });
                }else{
                    this.$state.go('app.fee.feeOrderDetailController',{ feeId:id, orderId:result.data.orderList[0], carrierOrderId:this.$scope.carrierOrderId, name:'SettleManage' });
                }
            });
        });
    }

    back=()=>{
        this.$state.go('app.fee.feeSettle_admin',{name:'feeSettle_admin'})
    }
}
angular.module("tms").controller( "feeSettleAuditDetailController" ,[ "$scope" , "feeService" , "$location" , "orderService" , "$ngBootbox" , "$state" , IFeeSettleAuditDetailController]);